package com.bzframework.pro.io.configuration;

import com.bzframework.pro.io.manager.DefaultFileManager;
import com.bzframework.pro.io.manager.FileManager;
import com.bzframework.pro.io.manager.TencentCosFileManager;
import com.bzframework.pro.io.service.FileEntityService;
import com.bzframework.pro.io.web.FileController;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.region.Region;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
 * @Author bingzhong
 * @Date 2024/12/6 15:14
 * @Description
 */
@Slf4j
@EnableConfigurationProperties({LocalFileProperties.class,CosFileProperties.class})
public class SystemFileConfiguration {

    @Bean
    @ConditionalOnProperty(prefix = "file.manager.cos", name = "enabled", havingValue = "true")
    public COSClient cosClient(CosFileProperties cosFileProperties) {
        // 1 初始化用户身份信息（secretId, secretKey）。
        // SECRETID 和 SECRETKEY 请登录访问管理控制台 https://console.cloud.tencent.com/cam/capi 进行查看和管理
        COSCredentials cred = new BasicCOSCredentials(cosFileProperties.getSecretId(), cosFileProperties.getSecretKey());
        // 2 设置 bucket 的地域, COS 地域的简称请参见 https://cloud.tencent.com/document/product/436/6224
        // clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
        Region region = new Region(cosFileProperties.getRegion());
        ClientConfig clientConfig = new ClientConfig(region);
        // 从 5.6.54 版本开始，默认使用了 https
        clientConfig.setHttpProtocol(HttpProtocol.https);
        // 3 生成 cos 客户端。
        COSClient cosClient = new COSClient(cred, clientConfig);
        return cosClient;
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = "file.manager.local", name = "enabled", havingValue = "true", matchIfMissing = true)
    public FileManager defaultFileManager(LocalFileProperties localFileProperties,
                                          FileEntityService fileEntityService) {
        log.info("DefaultFileManager is loaded");
        return new DefaultFileManager(localFileProperties, fileEntityService);
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = "file.manager.cos", name = "enabled", havingValue = "true")
    public FileManager tencentCosFileManager(CosFileProperties cosFileProperties,
                                             COSClient cosClient) {
        log.info("TencentCosFileManager is loaded");
        return new TencentCosFileManager(cosFileProperties, cosClient);
    }

    @Bean
    @ConditionalOnMissingBean
    public FileController fileController(FileManager fileManager) {
        return new FileController(fileManager);
    }

    @Bean
    @ConditionalOnMissingBean
    public FileEntityService fileEntityService() {
        return new FileEntityService();
    }

}
